package edu.uoc.tfc.web.tags;



import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang.StringUtils;



public class PasswordFieldTag extends AbstractFormFieldTag 
{

	private static final String PASSWORD_PROTECTED = "&lt;&lt;password&gt;&gt;";

    private static final long serialVersionUID = 1L;

	private boolean retrievePassword = false; 

	public final boolean getRetrievePassword() {
		return retrievePassword;
	}

	public final void setRetrievePassword(boolean retrievePassword) {
		this.retrievePassword = retrievePassword;
	}



	@Override
    protected void doStartFormField(JspWriter out) throws JspException
	{
		try
		{
			if (isWritable())
			{
				String showedValue = "";
				if (getRetrievePassword() && !StringUtils.isBlank(getValue()))
				{
					showedValue = getValue();
				}
				
		        write(out,"<input class=\"" + getClassMode() + 
		        		"\" type=\"password\" name=\"" + getName() + 
		        		"\" id=\"" + getId() + 
		        		"\" value=\"" + showedValue + "\" />");
			}
			else
			{
				String showedValue;
				if (StringUtils.isBlank(getValue()))
				{
					showedValue = "&nbsp;";
				}
				else
				{
					showedValue = PASSWORD_PROTECTED;
				}
				write(out,"<span class=\"" + getClassMode() + "\">" + showedValue + "</span>");
				write(out,"<input type=\"hidden\" name=\"" + getName() + "\" id=\"" + getId() + "\" value=\"" + getValue() + "\" />");
			}
		}
		catch (Exception ex)
		{
			String message = "Error generando Campo de texto: " + this.getProperty();
			throw new JspException(message);
		}
	}



	@Override
    protected void doEndFormField(JspWriter out) throws JspException
    {
		// No hacemos nada
    }



}
